package studentManage;

import java.util.ArrayList;
import java.util.Scanner;

public class MajorModel {

	private int mId;
	private String major;
	private String stuId;

	static Scanner scanner = new Scanner(System.in);

	public int getmId() {
		return mId;
	}

	public void setmId(int mId) {
		this.mId = mId;
	}

	public String getMajor() {
		return major;
	}

	public void setMajor(String major) {
		this.major = major;
	}

	public String getStuId() {
		return stuId;
	}

	public void setStuId(String stuId) {
		this.stuId = stuId;
	}

	// 全部专业信息
	public void searchAll(ArrayList<StudentModel> studentModels, ArrayList<MajorModel> majorModels) {
		if (majorModels.size() == 0) {
			System.out.println("没有任何专业！");
			return;
		}
		for (int i = 0; i < majorModels.size(); i++) {
			majorMes(studentModels, majorModels, i);
		}
	}

	// 根据id查找专业
	public int searchById(ArrayList<MajorModel> majorModels, int mid) {
		for (int i = 0; i < majorModels.size(); i++) {
			if (mid == majorModels.get(i).getmId()) {
				return i;
			}
		}
		return -1;
	}

	// 根据专业名查找专业
	public int searchByName(ArrayList<MajorModel> majorModels, String name) {
		for (int i = 0; i < majorModels.size(); i++) {
			if (majorModels.get(i).getMajor().equals(name)) {
				return i;
			}
		}
		return -1;
	}

	// 查
	public void majorMes(ArrayList<StudentModel> studentModels, ArrayList<MajorModel> majorModels, int i) {
		StudentModel studentModel = new StudentModel();
		if (i == -1) {
			System.out.println("没有查到相关专业！");
			System.out.println("*****************");
			return;
		}
		System.out.println("*****************");
		System.out.println("专业名：" + majorModels.get(i).getMajor());
		System.out.println("专业id：" + majorModels.get(i).getmId());

		System.out.print("本专业学生：");
		if (majorModels.get(i).getStuId() == null) {
			System.out.println("本专业暂无学生！");
		} else {
			String[] stu = majorModels.get(i).getStuId().trim().split(" ");
			for (String str : stu) {
				int sid = Integer.parseInt(str);
				int j = studentModel.searchStudent(studentModels, sid);// 得到学生下标
				System.out.println(studentModels.get(j).getName());
			}
		}
		System.out.println("*****************");

	}

	// 添加专业
	public void addMajor(ArrayList<MajorModel> majorModels) {
		MajorModel majorModel = new MajorModel();
		System.out.println("请输入专业名称：");
		String major = scanner.nextLine();
		majorModel.setMajor(major);

		System.out.println("请输入专业ID：");
		String maId = scanner.nextLine();
		int mId = Integer.parseInt(maId);
		majorModel.setmId(mId);

		majorModels.add(majorModel);
	}

	// 删除
	public void del(ArrayList<MajorModel> majorModels, int i) {
		if (i == -1) {
			System.out.println("没有该专业，删除失败！");
			return;
		}
		majorModels.remove(i);
		System.out.println("删除成功！");
	}

	// 修改
	public void updateMar(ArrayList<MajorModel> majorModels, int i) {
		if (i == -1) {
			System.out.println("没有该专业!");
			return;
		}
		System.out.println("专业名：" + majorModels.get(i).getMajor());
		System.out.println("请输入修改后的专业名：");
		String name = scanner.nextLine();
		majorModels.get(i).setMajor(name);

		System.out.println("专业id：" + majorModels.get(i).getmId());
		System.out.println("请输入修改后的专业id");
		String id = scanner.nextLine();
		int mid = Integer.parseInt(id);
		majorModels.get(i).setmId(mid);
	}
}
